//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "IborLeg.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CIborLeg::CIborLeg (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IIborIndex^ index) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CSchedule^ _Cschedule;
    CIborIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phIborLeg = NULL;
#endif
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        _ppIborLeg = new boost::shared_ptr<QuantLib::IborLeg> (new QuantLib::IborLeg ( _schedule,  _index ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Cashflows::CIborLeg::CIborLeg (boost::shared_ptr<QuantLib::IborLeg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIborLeg = NULL;
#endif
	_ppIborLeg = &childNative;
    
}
Cephei::QL::Cashflows::CIborLeg::CIborLeg (QuantLib::IborLeg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIborLeg = NULL;
#endif
	_ppIborLeg = new boost::shared_ptr<QuantLib::IborLeg> (&childNative);
    
    _IborLegOwner = owner;
    
}

Cephei::QL::Cashflows::CIborLeg::CIborLeg (CIborLeg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIborLeg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIborLeg = new boost::shared_ptr<QuantLib::IborLeg> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CIborLeg::CIborLeg (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIborLeg = NULL;
#endif
	if (!t->IsSubclassOf(CIborLeg::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CIborLeg::CIborLeg (QuantLib::Handle<QuantLib::IborLeg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIborLeg = &childNative;
	_ppIborLeg = &static_cast<boost::shared_ptr<QuantLib::IborLeg>>(childNative.currentLink());
    
    _IborLegOwner = owner;
}
Cephei::QL::Cashflows::CIborLeg::CIborLeg (QuantLib::Handle<QuantLib::IborLeg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIborLeg = &childNative;
	_ppIborLeg = &static_cast<boost::shared_ptr<QuantLib::IborLeg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CIborLeg::CIborLeg (QuantLib::IborLeg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIborLeg = NULL;
#endif
	_ppIborLeg = new boost::shared_ptr<QuantLib::IborLeg> (new QuantLib::IborLeg (childNative));
    
}
#endif

Cephei::QL::Cashflows::CIborLeg::~CIborLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIborLeg != NULL)
    {
	    delete _ppIborLeg;
        _ppIborLeg = NULL;
    }
}
Cephei::QL::Cashflows::CIborLeg::!CIborLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIborLeg != NULL)
    {
	    delete _ppIborLeg;
    }
}
QuantLib::IborLeg& Cephei::QL::Cashflows::CIborLeg::GetReference ()
{
    if (_ppIborLeg == NULL) throw REFNEW NativeNullException ();
	return **_ppIborLeg;
}
boost::shared_ptr<QuantLib::IborLeg>& Cephei::QL::Cashflows::CIborLeg::GetShared ()
{
    if (_ppIborLeg == NULL) throw REFNEW NativeNullException ();
	return *_ppIborLeg;
}
QuantLib::IborLeg* Cephei::QL::Cashflows::CIborLeg::GetPointer ()
{
    if (_ppIborLeg == NULL) throw REFNEW NativeNullException ();
	return &**_ppIborLeg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IborLeg>& Cephei::QL::Cashflows::CIborLeg::GetHandle ()
{
	if (_phIborLeg == NULL)
	{
		_phIborLeg = new Handle<QuantLib::IborLeg> (*_ppIborLeg);
	}
	return *_phIborLeg;
}
#endif
bool Cephei::QL::Cashflows::CIborLeg::HasNative () 
{
	return (_ppIborLeg != NULL);
}

Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::InArrears (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _flag = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (flag) ? (bool)ValueHelper::Convert (flag->Value) : true); //9a
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->inArrears ( _flag );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithCaps (Cephei::Core::IVector<Double>^ caps)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        caps->Lock();
        INativeVector<Double>^ _NCIcaps = caps->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcaps = safe_cast<CDoubleVector^>(_NCIcaps);
        std::vector<QuantLib::Rate>& _caps = static_cast<std::vector<QuantLib::Rate>&> (_NCcaps->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withCaps ( _caps );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (caps != nullptr) caps->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithFixingDays (Cephei::Core::IVector<UInt32>^ fixingDays)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        fixingDays->Lock();
        INativeVector<UInt32>^ _NCIfixingDays = fixingDays->getFeature (NativeFeature::Value);
        CUInt32Vector^ _NCfixingDays = safe_cast<CUInt32Vector^>(_NCIfixingDays);
        std::vector<QuantLib::Natural>& _fixingDays = static_cast<std::vector<QuantLib::Natural>&> (_NCfixingDays->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withFixingDays ( _fixingDays );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (fixingDays != nullptr) fixingDays->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithFloors (Cephei::Core::IVector<Double>^ floors)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        floors->Lock();
        INativeVector<Double>^ _NCIfloors = floors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloors = safe_cast<CDoubleVector^>(_NCIfloors);
        std::vector<QuantLib::Rate>& _floors = static_cast<std::vector<QuantLib::Rate>&> (_NCfloors->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withFloors ( _floors );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (floors != nullptr) floors->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithGearings (Cephei::Core::IVector<Double>^ gearings)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        gearings->Lock();
        INativeVector<Double>^ _NCIgearings = gearings->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgearings = safe_cast<CDoubleVector^>(_NCIgearings);
        std::vector<QuantLib::Real>& _gearings = static_cast<std::vector<QuantLib::Real>&> (_NCgearings->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withGearings ( _gearings );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (gearings != nullptr) gearings->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithNotionals (Cephei::Core::IVector<Double>^ notionals)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        notionals->Lock();
        INativeVector<Double>^ _NCInotionals = notionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withNotionals ( _notionals );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (notionals != nullptr) notionals->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithPaymentAdjustment (QL::Times::BusinessDayConventionEnum convention)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withPaymentAdjustment ( _convention );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithPaymentDayCounter (Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withPaymentDayCounter ( _dayCounter );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithSpreads (Cephei::Core::IVector<Double>^ spreads)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        spreads->Lock();
        INativeVector<Double>^ _NCIspreads = spreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        std::vector<QuantLib::Spread>& _spreads = static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ());
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withSpreads ( _spreads );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (spreads != nullptr) spreads->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg::WithZeroPayments (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _flag = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (flag) ? (bool)ValueHelper::Convert (flag->Value) : true); //9a
    	QuantLib::IborLeg& _rv = (QuantLib::IborLeg&)(*_ppIborLeg)->withZeroPayments ( _flag );   
        Cephei::QL::Cashflows::CIborLeg^ _nrv = REFNEW Cephei::QL::Cashflows::CIborLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IIborLeg^ Cephei::QL::Cashflows::CIborLeg_Factory::Create (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IIborIndex^ index)
{
    return REFNEW CIborLeg ( schedule,  index);
}
